package com.musemun.service.function.functionimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.musemun.common.ResultCode;
import com.musemun.exception.ApiException;
import com.musemun.pojo.Crifo;
import com.musemun.pojo.Cropifo;
import com.musemun.pojo.Vo.*;
import com.musemun.service.CrifoService;
import com.musemun.service.CropifoService;
import com.musemun.service.ImageService;
import com.musemun.service.function.CulturalRelic;
import com.sun.scenario.effect.Crop;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author winner
 * @description: 文物类
 * @date 2020/4/19
 */
@Service
public class CulturalRelicImpl implements CulturalRelic {

    //图片操作字段
    @Resource
    private ImageService imageService;

    //文物表操作字段
    @Resource
    private CrifoService crifoService;
    //文物操作记录表操作字段
    @Resource
    private CropifoService cropifoService;

    public void setImageService(ImageService imageService) {
        this.imageService = imageService;
    }

    public void setCrifoService(CrifoService crifoService) {
        this.crifoService = crifoService;
    }

    @Override
    @Transactional
    public Map<String, Object> getCrifo(Long crNo) {
        Crifo crifo = crifoService.getById(crNo);
        if (crifo == null) {
            throw new ApiException(ResultCode.ERROR);
        }
        CrifoPageVo crifoPageVo = new CrifoPageVo();
        BeanUtils.copyProperties(crifo, crifoPageVo);
        crifoPageVo.setIsSet(false);
        Map<String, Object> map = new HashMap<>(16);
        map.put("crifoVo", crifoPageVo);
        return map;

    }

    @Override
    public String insert(Crifo crifo) {
        if (crifoService.save(crifo)) {
            return "新增成功";
        }
        return "新增失败";
    }

    @Override
    @Transactional
    public String update(Crifo crifo) {
        System.out.println("zy::::" + crifo.toString());
        if (crifoService.getById(crifo.getCrNo()) == null)
            throw new ApiException(ResultCode.ERROR);
        if (crifoService.updateById(crifo)) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    @Transactional
    public String deleteByNo(Long crNo) {
        if (crifoService.getById(crNo) == null)
            throw new ApiException(ResultCode.ERROR);
        if (crifoService.removeById(crNo))
            return "删除成功";
        return "删除失败";
    }

    @Override
    @Transactional
    public String deleteByName(String crName) {
        LambdaQueryWrapper<Crifo> lambda = new QueryWrapper<Crifo>().lambda().eq(Crifo::getCrName, crName);
        if (crifoService.getOne(lambda) == null)
            throw new ApiException(ResultCode.ERROR);
        if (crifoService.remove(lambda))
            return "删除成功";
        return "删除失败";
    }

    @Override
    @Transactional
    public String deleteByAddr(String crAddr) {
        LambdaQueryWrapper<Crifo> lambda = new QueryWrapper<Crifo>().lambda().eq(Crifo::getCrAddr, crAddr);
        if (crifoService.getOne(lambda) == null)
            throw new ApiException(ResultCode.ERROR);
        if (crifoService.remove(lambda))
            return "删除成功";
        return "删除失败";
    }

    @Override
    @Cacheable(cacheNames = "crifoListVo")
    public CrifoListVo getListByStatus(String crState) {
        LambdaQueryWrapper<Crifo> lambda = new QueryWrapper<Crifo>().lambda().eq(Crifo::getCrState, crState);
        List<Crifo> crifos = crifoService.list(lambda);
        CrifoListVo crifoListVo = new CrifoListVo();
        crifoListVo.setCrifoList(crifos);
        return crifoListVo;
    }

    @Override
    @Cacheable(cacheNames = "dropDownList")
    public DropDownListVo getDownList() {
        DropDownListVo dropDownListVo = new DropDownListVo();
        QueryWrapper<Crifo> state = new QueryWrapper<Crifo>().select("distinct crState").isNotNull("crState");
        QueryWrapper<Crifo> house = new QueryWrapper<Crifo>().select("distinct crWarehouse").isNotNull("crWarehouse");
        QueryWrapper<Crifo> year = new QueryWrapper<Crifo>().select("distinct crYear").isNotNull("crYear");
        QueryWrapper<Crifo> addr = new QueryWrapper<Crifo>().select("distinct crAddr").isNotNull("crAddr").isNotNull("crAddr");
        QueryWrapper<Crifo> type = new QueryWrapper<Crifo>().select("distinct crType").isNotNull("crType");
        dropDownListVo.setAddrList(crifoService.list(addr).stream().map((x) -> x.getCrAddr()).collect(Collectors.toList()));
        dropDownListVo.setHouseList(crifoService.list(house).stream().map((x) -> x.getCrWarehouse()).collect(Collectors.toList()));
        dropDownListVo.setTypeList(crifoService.list(type).stream().map((x) -> x.getCrType()).collect(Collectors.toList()));
        dropDownListVo.setYearList(crifoService.list(year).stream().map((x) -> x.getCrYear()).collect(Collectors.toList()));
        dropDownListVo.setStateList(crifoService.list(state).stream().map((x) -> x.getCrState()).collect(Collectors.toList()));
        return dropDownListVo;
    }

    @Override
    @Cacheable(cacheNames = "crifoUnList")
    public Object getUnList(Integer pageNum, Integer pageSize) {
        Page<Crifo> page = new Page<>(pageNum, pageSize);

        IPage<Crifo> crifoIPage = crifoService.page(page, null);
        return crifoIPage;
    }

    @Override
//    @Cacheable(cacheNames = "crifoList")
    public Object getList(Integer pageNum, Integer pageSize, CombineQueryVo combineQueryVo, Boolean sort) {
        Page<Crifo> page = new Page<>(pageNum, pageSize);
        IPage<Crifo> crifoIPage = null;
        LambdaQueryWrapper<Crifo> queryWrapper = new QueryWrapper<Crifo>().lambda();
//        System.out.println(combineQueryVo.getSaveDate()[0]);
        if (combineQueryVo != null) {
            if (combineQueryVo.getSaveDate() != null) {
                queryWrapper.between(Crifo::getCrSaveDate, combineQueryVo.getSaveDate()[0], combineQueryVo.getSaveDate()[1]);
            }
            if (combineQueryVo.getRecordDate() != null) {
                queryWrapper.between(Crifo::getCrRecordDate, combineQueryVo.getRecordDate()[0], combineQueryVo.getRecordDate()[1]);
            }
            if (combineQueryVo.getStatus() != null) {
                queryWrapper.eq(Crifo::getCrState, combineQueryVo.getStatus());
            }
            if (combineQueryVo.getAddr() != null) {
                queryWrapper.eq(Crifo::getCrAddr, combineQueryVo.getAddr());
            }
            if (combineQueryVo.getHouse() != null) {
                queryWrapper.eq(Crifo::getCrWarehouse, combineQueryVo.getHouse());
            }
            if (combineQueryVo.getType() != null) {
                queryWrapper.eq(Crifo::getCrType, combineQueryVo.getType());
            }
            if (combineQueryVo.getYear() != null) {
                queryWrapper.eq(Crifo::getCrYear, combineQueryVo.getYear());
            }
            crifoIPage = crifoService.page(page, queryWrapper);


        } else {
            crifoIPage = crifoService.page(page, null);
        }

        List<CrifoPageVo> crifoPageVoList = new ArrayList<>(10);
        for (
                Crifo c : crifoIPage.getRecords()
        ) {
            CrifoPageVo crifoPageVo = new CrifoPageVo();
            BeanUtils.copyProperties(c, crifoPageVo);
            crifoPageVo.setIsSet(false);
            crifoPageVoList.add(crifoPageVo);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("records", crifoPageVoList);
        map.put("total", crifoIPage.getTotal());
        map.put("current", crifoIPage.getCurrent());
        map.put("size", crifoIPage.getSize());
        map.put("pages", crifoIPage.getPages());
        return map;
    }

    @Override
//    @Cacheable(cacheNames = "totalList")
    public Map<String, Object> list() {
        CrifoListVo crifoListVo = new CrifoListVo();
        crifoListVo.setCrifoList(crifoService.list(null));
        int total = crifoService.count(null);
        Map<String, Object> hashMap = new HashMap<>(16);
        hashMap.put("crifoList", crifoListVo.getCrifoList());
        hashMap.put("total", total);
        return hashMap;
    }

    @Override
//    @Cacheable(cacheNames = "cropifoListVo")
    public CropifoListVo selectCrop() {
        CropifoListVo cropifoListVo = new CropifoListVo();
        cropifoListVo.setCrifoVo(cropifoService.list(null));
        return cropifoListVo;
    }

    @Override
    @Cacheable(cacheNames = "cropifoList")
    public List<CropifoVo> excelExport() {
        List<CropifoVo> cropifoVoList = new ArrayList<>(50);
        List<Cropifo> cropifoList = cropifoService.list(new QueryWrapper<>());
        for (Cropifo c : cropifoList
        ) {
            CropifoVo cropifoVo = new CropifoVo();
            BeanUtils.copyProperties(c, cropifoVo);
            cropifoVo.setEeNo(c.getENo());
            cropifoVoList.add(cropifoVo);
        }
        return cropifoVoList;
    }

    @Override
//    @Cacheable(cacheNames = "cropifoListVo")
    public CropifoListVo selectCropByTime(CropQueryVo cropQueryVo) {
//        System.out.println(cropQueryVo.getTest());
//        System.out.println(cropQueryVo.getOpDate()[0]);
        LambdaQueryWrapper<Cropifo> lambdaQueryWrapper = new QueryWrapper<Cropifo>().lambda().eq(Cropifo::getOpType, cropQueryVo.getOpType()).between(Cropifo::getOpDate, cropQueryVo.getOpDate()[0], cropQueryVo.getOpDate()[1]);
        CropifoListVo cropifoListVo = new CropifoListVo();
        cropifoListVo.setCrifoVo(cropifoService.list(lambdaQueryWrapper));

        return cropifoListVo;
    }

    @Override
    @Transactional
    public String deleteByCrop(Cropifo cropifo) {

        LambdaQueryWrapper<Cropifo> queryWrapper = new QueryWrapper<Cropifo>().lambda()
                .eq(Cropifo::getOpDate, cropifo.getOpDate())
                .eq(Cropifo::getOpType, cropifo.getOpType())
                .eq(Cropifo::getCrNo, cropifo.getCrNo())
                .eq(Cropifo::getENo, cropifo.getENo());
        List<Cropifo> cropifoList = cropifoService.list(queryWrapper);
        if (cropifoList.size() != 0) {
            cropifoService.remove(queryWrapper);
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    @Transactional
    public String updateStatus(Long crNo, String crWarehouse, String crAddr, int number) {
        Crifo crifo = crifoService.getById(crNo);
        if (number == 0) {

            if ("入库".equals(crifo.getCrState())) {
                return "已经入库了无需入库";
            }
            crifo.setCrRecordDate(null);
            if (crifo == null) {
                throw new ApiException(ResultCode.ERROR);
            }
            crifo.setCrState("入库");
            crifo.setCrWarehouse(crWarehouse);
            crifo.setCrAddr(crAddr);
            crifoService.updateById(crifo);
            cropifoService.insertCropifo(crifo, 1);
            return "入库成功";
        } else {
            crifo.setCrRecordDate(null);
            if (crifo == null) {
                throw new ApiException(ResultCode.ERROR);
            }
            crifo.setCrWarehouse(crWarehouse);
            crifo.setCrAddr(crAddr);
            crifoService.updateById(crifo);
            return "移动成功";
        }

    }
}
